#include "../include/Good.h"
#include<sstream>
#include <fstream>
#include <iomanip>

inline const std::string convert(int i) {
  std::string res = "F";
  std::string cut = std::to_string(i);
  res += std::string(4 - cut.length(), '0');
  res += cut;
  return res;
}

std::ostream &operator<<(std::ostream &os, good g) {
  os << std::setiosflags(std::ios::left) << std::setw(14) << convert(g.id);
  os << std::setw(14) << g.name << '\t' << std::setw(14) << g.brand << '\t'
     << std::setw(14) << g.price << '\t' << std::setw(14) << g.quantity << '\t'
     << std::setw(14) << g.owner;
  os << std::resetiosflags(std::ios::left);
  return os;
}

bool good::operator==(const good &ot) {
  return ot.brand == brand && ot.name == name && ot.owner == owner;
}

void good::to_file(std::ofstream &fs) { fs << *this << std::endl; }

void good::from_file(std::ifstream &fs) {
  std::string trash;
  fs >> trash;
  sscanf(trash.c_str(), "F%hd", &id);
  fs >> name >> brand >> price >> quantity;
  // std::cout << *this << std::endl;
}

void good::from_stringstream(std::istringstream& fs) {
  std::string trash;
  fs >> trash;
  sscanf(trash.c_str(), "F%hd", &id);
  fs >> name >> brand >> price >> quantity;
  // std::cout << *this << std::endl;
}

std::istream &operator>>(std::istream &is, good &g) {
  std::string trash;
  is >> trash;
  sscanf(trash.c_str(), "F%hd", &g.id);
  return is >> g.name >> g.brand >> g.price >> g.quantity >> g.owner;
}